package com.study.lib_tiandi_map.tiandimap

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.webkit.WebChromeClient
import android.webkit.WebView
import com.google.gson.Gson
import com.study.lib_tiandi_map.jsBridge.JsFun
import com.study.lib_tiandi_map.interfaces.*

class MapView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
) : WebView(context, attrs, defStyleAttr), MapControl {

    companion object {
        private var devKey = ""
        fun setDevKey(devKey: String) {
            Companion.devKey = devKey
        }
    }

    init {
        initParam()
        //需要配置天地图的开发key
        loadUrl("file:///android_asset/map.html?tk=$devKey")
    }

    /**
     * 初始化操作
     */
    @SuppressLint("SetJavaScriptEnabled")
    private fun initParam() {
        settings.apply {
            domStorageEnabled = true
            mixedContentMode = 0
            allowFileAccess = false
            javaScriptEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
            webChromeClient = object : WebChromeClient() {

            }
        }
        //添加js调用native的对象
        addJavascriptInterface(JsFun(this), "JsFun")
    }

    /**
     * 地图加载完成监听
     */
    override var onMapLoadComplete: OnMapLoadComplete = object : OnMapLoadComplete {
        override fun onComplete() {
            Log.d("加载完成", "地图加载完成回调")
        }
    }

    override var onMapClick: OnMapClick = object : OnMapClick {
        override fun onClick(lng: Double, lat: Double) {
            Log.d("地图点击", "lng:$lng lat:$lat")
        }
    }

    override var getAreaPoints: GetAreaPoints = object : GetAreaPoints {
        override fun points(points: List<Point>) {
            Log.d("获取平面的坐标点", Gson().toJson(points))
        }
    }

    override var getArea: GetArea = object : GetArea {
        override fun area(area: String) {
            Log.d("获取平面面积", area)
        }
    }
    override var onPolygonClick: OnPolygonClick = object : OnPolygonClick {
        override fun onClick(tag: String) {
            Log.d("点击了面", tag)
        }

    }

    /**
     * 放大
     */
    override fun zoomToLarge() {
        post { loadUrl("javascript:zoomToLarge()") }
    }

    /**
     * 缩小
     */
    override fun zoomToSmall() {
        post { loadUrl("javascript:zoomToSmall()") }
    }

    /**
     * 放大缩小到固定级别
     */
    override fun zoomTo(zoomLevel: Int) {
        post { loadUrl("javascript:zoomTo($zoomLevel)") }
    }

    /**
     * 获取地图中心点
     */
    override fun getCenter(callBack: MapCenterCallBack) {
        evaluateJavascript("javascript:getCenter()") {
            val lng = it.split("#")[0].replace("\"", "").toDouble()
            val lat = it.split("#")[1].replace("\"", "").toDouble()

            callBack.center(lng, lat)
        }
    }

    /**
     * 获取地图缩放级别
     */
    override fun getZoom(callBack: MapZoomCallBack) {
        evaluateJavascript("javascript:getZoom()") {
            callBack.zoom(it.toInt())
        }
    }

    /**
     * 添加infoWindow
     */
    override fun addInfoWindow(lng: Double, lat: Double, msg: String) {
        post { loadUrl("javascript:addInfoWindow($lng,$lat,\"$msg\")") }
    }

    /**
     * 添加标注
     */
    override fun addMarker(lng: Double, lat: Double) {
        post { loadUrl("javascript:addMarker($lng,$lat)") }
    }

    /**
     * 画线
     */
    override fun drawLine(
        points: List<Point>, color: String,
        weight: Int, isEdit: Boolean, tag: String
    ) {
        val param = Gson().toJson(points)
        post { loadUrl("javascript:drawLine($param,\"$color\",$weight,$isEdit,\"$tag\")") }
    }

    /**
     * 画平面
     *
     */
    override fun drawArea(
        points: List<Point>,
        color: String,
        weight: Int,
        opacity: Float,
        fillColor: String,
        fillOpacity: Float,
        isEdit: Boolean,
        tag: String
    ) {
        val param = Gson().toJson(points)
        post { loadUrl("javascript:drawArea($param,\"$color\",$weight,$opacity,\"$fillColor\",$fillOpacity,$isEdit,\"$tag\")") }
    }

    /**
     * 清除所有标记图层
     */
    override fun clearAllLayer() {
        post { loadUrl("javascript:clearAllLayer()") }
    }

    /**
     * 定位当前位置
     */
    override fun location() {
        val location = GetLocation(context).location()
        location?.let {
            setCenter(it.longitude, it.latitude)
        }
    }

    /**
     * 设置地图中心点位置
     */
    override fun setCenter(lng: Double, lat: Double, zoom: Int) {
        post { loadUrl("javascript:setCenter($lng,$lat,$zoom)") }
    }

    /**
     * 在地图上添加一个圆点
     */
    override fun addPoint(lng: Double, lat: Double) {
        post { loadUrl("javascript:addPoint($lng,$lat)") }
    }

    /**
     * 添加加载监听
     */
    override fun setOnMapLoadListener(onMapLoadComplete: OnMapLoadComplete) {
        this.onMapLoadComplete = onMapLoadComplete
    }

    /**
     * 设置点击事件监听
     */
    override fun setOnMapClickListener(onMapClick: OnMapClick) {
        this.onMapClick = onMapClick
        post { loadUrl("javascript:addMapClick()") }
    }

    /**
     * 添加地图覆盖物面的点击事件监听
     */
    override fun addOnPolygonClickListener(onPolygonClick: OnPolygonClick) {
        this.onPolygonClick = onPolygonClick
        post { loadUrl("javascript:addPolygonClick()") }
    }

    /**
     * 移除点击事件
     */
    override fun removeMapClick() {
        post { loadUrl("javascript:removeMapClick()") }
    }

    /**
     * 获取平面的所有的点根据tag
     */
    override fun geAreaPointsByTag(tag: String, getAreaPoints: GetAreaPoints) {
        this.getAreaPoints = getAreaPoints
        post { loadUrl("javascript:geAreaPointsByTag(\"$tag\")") }
    }

    /**
     * 获取平面面积
     */
    override fun getPolygonArea(points: List<Point>, getArea: GetArea) {
        this.getArea = getArea
        val param = Gson().toJson(points)
        post { loadUrl("javascript:getArea($param)") }
    }

    /**
     * 根据tag隐藏覆盖物
     */
    override fun hideOverLayByTag(tag: String) {
        post { loadUrl("javascript:hideOverLayByTag(\"$tag\")") }
    }

    /**
     * 根据tag展示覆盖物
     */
    override fun showOverLayByTag(tag: String) {
        post { loadUrl("javascript:showOverLayByTag(\"$tag\")") }
    }
}